<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/5
 * Time: 17:26
 *
 * 获取输入的参数
 * 根据验证方法不同,自动选择使用解密还是md5验证
 */

namespace app\common\model;


use think\exception\HttpException;

class Input
{
    protected static $input;
    public static $token;
    public static $crypt;
    public static $clientId;

    public function __construct()
    {
        solveDomain();

        $dataAll=$this->input();

        self::$input=$dataAll;
        self::$token=$this->input('token','');
        self::$crypt=$this->input('crypt','');



        $crypt=strtolower($this->input('crypt',''));
        if(defined('CLIENTID')){
            $clientId=CLIENTID;
        }else{
            $clientId=$this->input('clientId',0);
        }


        switch($crypt){
            case 'miyu':
                $data=isset($dataAll['data'])?trim($dataAll['data']):'';
                $token=isset($dataAll['token'])?trim($dataAll['token']):'';
                $key=Crypt::getKey($token);
                if($data){
                    unset(self::$input['data']);
                    $deData=Crypt::decrypt($data,$key);
                    if($deData){
                        $dataArr=json_decode($deData,true);
                        if(is_array($dataArr)){
                            foreach($dataArr as $k=>$v){
                                self::$input[$k]=$v;
                                $key_name=humpToLine($k);//驼峰法的转成下划线再存一份
                                if($key_name!=$k)self::$input[$key_name]=$v;
                            }
                        }else{
                            throw new HttpException(200,'解密失败,或data参数格式不是数组,请核实token是否正确');
                        }
                    }else{
                        throw new HttpException(200,'解密失败');
                    }
                }
                self::$input['token']=$token;

                break;
            case 'md5':
                $data=$dataAll;
                if(Verify::verify($data)){
                    foreach ($data as $k=>$v) {
                        $data[$k]=urldecode($v);
                        $key_name=humpToLine($k);//驼峰法的转成下划线再存一份
                        if($key_name!=$k)$data[$key_name]=$v;
                    }
                    self::$input=$data;
                }else{
                    throw new HttpException(200,'签名验证失败');
                }
                break;
            case 'none';
                if(isset($dataAll['data'])){
                    $data=json_decode($dataAll['data'],true);
                    if(!empty($data)){
                        self::$input=$data;
                    }
                }

                self::$input=$dataAll;
                foreach($dataAll as $k=>$v){
                    self::$input[$k]=$v;
                    $key_name=humpToLine($k);//驼峰法的转成下划线再存一份
                    if($key_name!=$k)self::$input[$key_name]=$v;
                }
                break;
            default:
                self::$input=$dataAll;

        }

        self::$input['crypt']=$crypt;
        self::$input['clientId']=$clientId;
        Log::record(json_encode(self::$input,JSON_UNESCAPED_UNICODE),'INPUT');

    }

    /**
     * 获取参数
     * @param $name
     * @return string
     */
    static public function get($name,$default = null){
        $value=isset(self::$input[$name])?self::$input[$name]:$default;
        return $value;
    }

    /**
     * 设置输入参数
     * @param $name
     * @param $value
     */
    static public function set($name,$value=''){
        if(is_array($name)){
            self::$input=array_merge(self::$input,$name);
        }else{
            self::$input[$name]=$value;
        }
    }

    /**
     * 获取所有输入参数
     * @return mixed 成功返回数组
     */
    static public function getAll(){
        return self::$input;
    }

    static public function getPlatId(){
        if(!isset(self::$input['clientId'])){
            Out::exception('clientId不能为空');
        }
        $redis=new Redis();
        $node=$redis->hGetAll('common:client_node:'.self::$input['clientId']);
        if($node){
            return $node['plat_id'];
        }else{
            $node=\think\Db::name('client_node')->where(['client_id'=>self::$input['clientId']])->find();
            if($node){
                $redis->hMset('common:client_node:'.self::$input['clientId'],$node);
                return $node['plat_id'];
            }else{
                Out::exception('clientId不存在:'.self::$input['clientId']);
            }
        }
    }

    static public function input($key = '', $default = null, $filter = '')
    {
        if (0 === strpos($key, '?')) {
            $key = substr($key, 1);
            $has = true;
        }
        if ($pos = strpos($key, '.')) {
            // 指定参数来源
            list($method, $key) = explode('.', $key, 2);
            if (!in_array($method, ['get', 'post', 'put', 'patch', 'delete', 'param', 'request', 'session', 'cookie', 'server', 'env', 'path', 'file'])) {
                $key    = $method . '.' . $key;
                $method = 'param';
            }
        } else {
            // 默认为自动判断
            $method = 'param';
        }
        if (isset($has)) {
            return request()->has($key, $method, $default);
        } else {
            return request()->$method($key, $default, $filter);
        }
    }
    static public function getClientId(){
        if(defined('CLIENTID')){
            $clientId=CLIENTID;
        }else{
            $clientId=self::get('clientId',0);
        }
        return $clientId;
    }




}